# @shuaige : 陈世玉
# @name :csv.py
# @time :2024/12/6 14:45
import csv

# 定义输入和输出文件路径
input_file_path = 'data.txt'
output_file_path = 'university_data.csv'

# 打开输入文件并读取内容
with open(input_file_path, 'r', encoding='utf-8') as file:
    lines = file.readlines()

# 打开输出CSV文件并写入数据
with open(output_file_path, 'w', newline='', encoding='utf-8') as csvfile:
    csvwriter = csv.writer(csvfile)

    # 写入CSV文件的表头
    csvwriter.writerow(['世界排名', '名称', '国家', '国家排名', '总分', '校友获奖'])

    # 遍历每一行数据并写入CSV文件
    for line in lines[0:]:
        data = line.strip().split(',')
        if len(data) == 6:
            csvwriter.writerow(data)

print(f"数据已成功转换并保存到 {output_file_path}")
